package com.summer.web.controller;

import com.summer.web.domain.User;
import com.summer.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/users")
public class UserController {

	@Autowired
	private UserService userService;
	

	@ApiIgnore
	@RequestMapping("/getUserById")
	@Cacheable(value="user",key="\"user_\" + #user.userId")
	public User getUserById(User user){
		System.out.println("从数据库中查询用户数据");
		return userService.getUserById(Long.valueOf(user.getUserId()));
	}

	@ApiIgnore
	@RequestMapping("/deleteUserById")
	@CacheEvict(value="user",key="\"user_\" + #userId")
	public void deleteUserById(String userId){
		userService.deleteUserById(Long.valueOf(userId));
	}

	
	@ApiOperation(value="获取用户列表",notes="")
	@RequestMapping(method=RequestMethod.GET)
	public List<User> getUserList(){
		List<User> list = Lists.newArrayList();
		
		User u = userService.getUserById(1);
		list.add(u);
		list.add(u);
		list.add(u);

		return list;
	}
	
	@ApiOperation(value="创建用户",notes="根据user对象创建用户")
	@ApiImplicitParam(name="user",value="用户详细实体user",required=true,dataType="User")
	@RequestMapping(method=RequestMethod.POST)
	public String postUser(User user){
		
		userService.insertUser(user);
		
		return "success";
	}
	
	@ApiOperation(value="获取用户详细信息",notes="根据url的userId来获取用户的详细信息")
	@ApiImplicitParam(name="userId",value="用户userId",required=true,dataType="String" , paramType="path")
	@RequestMapping(value="/{userId}",method=RequestMethod.GET)
	public User getUser(@PathVariable String userId){
		return  userService.getUserById(Long.valueOf(userId));
	}
	
	
	@ApiOperation(value="更新用户详细信息", notes="根据url的userId来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户userId", required = true, dataType = "String" ,paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @RequestMapping(value="/{userId}", method=RequestMethod.PUT)
    public String putUser(@PathVariable String userId,User user) {
        return "success";
    }

    @ApiOperation(value="删除用户", notes="根据url的useId来指定删除对象")
    @ApiImplicitParam(name = "userId", value = "用户userId", required = true, dataType = "String",paramType = "path")
    @RequestMapping(value="/{userId}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable String userId) {

		System.out.println("删除用户：" + userId);
		userService.deleteUserById(Long.valueOf(userId));

        return "success";
    }


}
